<?php
namespace Operationtest\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use Operationtest\Classe\WhereQueryBySelection;
use Operationtest\Classe\OperationDate;
use Operationtest\Classe\Table;
use Operationtest\Classe\ExcelManager;

class SyntheseController extends AbstractActionController
{
    protected $saleheadTable;
    protected $salerowTable;
    protected $storeTable;
    protected $meteoTable;
    protected $storecountTable;
    protected $marcheTable;
    protected $marcheStoreTable;
    protected $rhdataTable;
    
    public function setEventManager(EventManagerInterface $events) {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/operationtest');
        }, 100);
 
        return $this;
    }
    
    public function indexAction(){
        $table = new Table($sm = $this->getServiceLocator());
        $operationDate = new OperationDate();
        
        $request = $this->getRequest();
        $tableArray = array(
            'marcheStoreTable' => $table->getMarchestoreTable(),
            'marcheTable' => $table->getMarcheTable(),
            'storeTable' => $table->getStoreTable()
        );
        
        $whereQueryBySelection = new WhereQueryBySelection($tableArray);
        
        if($request->isPost()) {
            // CLAUSE WHERE
            $defaultWhere = $whereQueryBySelection->queryForSynthese($request);
            $otherWhere = $whereQueryBySelection->queryForSynthese($request, 'date');
            
            // CLAUSE GROUPBY
            $groupby = " GROUP BY saleHead.orderDate";            
            $ecgroupby = " GROUP BY date";            
            $rptgroupby = " GROUP BY saleHead.orderDate, Article.idArticle";
            $tmgroupby = " GROUP BY saleHead.orderDate"; 
            $tvagroupbay = "GROUP BY saleHead.orderDate, saleRow.taxeRate";
            
            $groupby2 = " GROUP BY saleHead.idStore, saleHead.orderDate";
            $ecgroupby2 = " GROUP BY idStore, date";
            
            // MISE EN SESSION DES VALEURS POUR LES RECUPERER DANS UNE AUTRE ACTION
            $dataSynthese = new Container('dataSynthese');
            $dataSynthese->request = $request->getPost();
            
            $dataSynthese->period = $operationDate->displayPeriod($request);
            $dataSynthese->period2 = $operationDate->displayPeriod2($request);
            $dataSynthese->store = $whereQueryBySelection->getTabStores($request);
            
            $dataSynthese->ca = $table->getSaleheadTable()->getSynthCa($defaultWhere, $groupby);   
            $dataSynthese->vtva = $table->getSaleheadTable()->getSynthVentilationTva($defaultWhere, $tvagroupbay);
            $dataSynthese->tm = $table->getSaleheadTable()->getSynthTmTtc($defaultWhere, $tmgroupby);
            $dataSynthese->nbT = $table->getSaleheadTable()->getSynthTransaction($defaultWhere, $groupby);            
            $dataSynthese->nbA = $table->getSalerowTable()->getNbArticle($defaultWhere, $groupby);
            $dataSynthese->rptProduit = $table->getSalerowTable()->getRapportProduit($defaultWhere, $rptgroupby); 
            $dataSynthese->meteoDays = $table->getMeteoTable()->getSMeteos($otherWhere);
            $dataSynthese->ecDays = $table->getStorecountTable()->getSynthEcarts($otherWhere, $ecgroupby);            
            $dataSynthese->repasDays = $table->getSaleheadTable()->getSynthRepas($defaultWhere, $groupby);
            $dataSynthese->ac = $table->getSaleheadTable()->getSynthAnnulationCaisse($defaultWhere, $groupby);
            $dataSynthese->banque = $table->getStorecountTable()->getSynthBanques($otherWhere, $ecgroupby);  
            $dataSynthese->rhdata = $table->getRhdataTable()->getSynthRhdata($otherWhere, $ecgroupby);  
            
            $dataSynthese->ca2 = $table->getSaleheadTable()->getSynthCa($defaultWhere, $groupby2);
            $dataSynthese->nbT2 = $table->getSaleheadTable()->getSynthTransaction($defaultWhere, $groupby2);
            $dataSynthese->nbA2 = $table->getSalerowTable()->getNbArticle($defaultWhere, $groupby2);
            $dataSynthese->ecDays2 = $table->getStorecountTable()->getSynthEcarts($otherWhere, $ecgroupby2);
            $dataSynthese->repasDays2 = $table->getSaleheadTable()->getSynthRepas($defaultWhere, $groupby2);
            $dataSynthese->ac2 = $table->getSaleheadTable()->getSynthAnnulationCaisse($defaultWhere, $groupby2);
            $dataSynthese->banque2 = $table->getStorecountTable()->getSynthBanques($otherWhere, $ecgroupby2);
            $dataSynthese->rhdata2 = $table->getRhdataTable()->getSynthRhdata($otherWhere, $ecgroupby2);
            
            // BUDGETS ///////////////////////////////////////////////////////////////////////////
            $budgetWhere = $whereQueryBySelection->budgetQueryForTableauDeBord($request, 'month');
            $budgetResult = $table->getBudgetTable()->getBudgetForTableauDeBord($budgetWhere);
            $budgets = $budgetTmp = array();
            foreach($budgetResult as $budget) {
                $budgetTmp[$budget->month][$budget->idStore]['totttc'] = $budget->totttc;
                $budgetTmp[$budget->month][$budget->idStore]['tm'] = $budget->tm;
                $budgetTmp[$budget->month][$budget->idStore]['tttxA'] = $budget->txA + $budget->txAl;
                $budgetTmp[$budget->month][$budget->idStore]['txE'] = $budget->txE;
            }
            
            $countBudgetTmp = 1;
            foreach($budgetTmp as $idxtmp => $tmp) {
                foreach($tmp as $idstore) {                    
                    if($countBudgetTmp == count($budgetTmp)) {
                        @$budgets['cattc'] += $idstore['totttc'];
                        @$budgets['caht'] += (($idstore['totttc']*0.995/1.07)+($idstore['totttc']*0.005/1.196));
                        @$budgets['nbt'] += ($idstore['totttc']/$idstore['tm']);
                        @$budgets['tttxA'] += $idstore['tttxA'];
                        @$budgets['txE'] += $idstore['txE'];
                    }
                }
                $countBudgetTmp++;
            }
            $dataSynthese->budgets = $budgets;
            //var_dump($budgets);
            
            return new ViewModel(array(
                'monthText' => $operationDate->tabMonthText(),

                ///////// PERIOD
                'periodText' => $operationDate->displayPeriodText($request),
                'period' => $operationDate->displayPeriod($request),

                ///////// STORE
                'store' => $whereQueryBySelection->getStoresNames($request),
                
            ));
        } else {
            return $this->redirect()->toRoute('operationtest/default');
        }
    }
    
    public function syntheseAction() {
        $dataSynthese = new Container('dataSynthese');
        
        // Get value for month and week
        $monthData = $weekData = array();
        foreach ($dataSynthese->offsetGet('period2') as $idxm => $month) {
            foreach ($month as $idxw => $week) {
                foreach ($week as $idxd => $day) {
                    // CA
                    if(!isset($weekData[$idxm][$idxw]['ca_ttc'])) $weekData[$idxm][$idxw]['ca_ttc'] = 0; 
                    if(!isset($weekData[$idxm][$idxw]['ca_ht'])) $weekData[$idxm][$idxw]['ca_ht'] = 0;
                    if(!isset($monthData[$idxm]['ca_ttc'])) $monthData[$idxm]['ca_ttc'] = 0;
                    if(!isset($monthData[$idxm]['ca_ht'])) $monthData[$idxm]['ca_ht'] = 0;
                        
                    foreach($dataSynthese->offsetGet('ca') as $ca_day) {
                        $orderDate = new \DateTime($ca_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['ca_ttc'] += $ca_day['ca_ttc'];
                            $weekData[$idxm][$idxw]['ca_ht'] += $ca_day['ca_ht'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['ca_ttc'] += $ca_day['ca_ttc'];
                            $monthData[$idxm]['ca_ht'] += $ca_day['ca_ht'];
                        }
                    }
                    
                    // TRANSACTION
                    if(!isset($weekData[$idxm][$idxw]['nbT'])) $weekData[$idxm][$idxw]['nbT'] = 0; 
                    if(!isset($monthData[$idxm]['nbT'])) $monthData[$idxm]['nbT'] = 0;
                    
                    foreach($dataSynthese->offsetGet('nbT') as $nbt_day) {
                        $orderDate = new \DateTime($nbt_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['nbT'] += $nbt_day['nbTransaction'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['nbT'] += $nbt_day['nbTransaction'];
                        }
                    }
                    
                    // ARTICLE
                    if(!isset($weekData[$idxm][$idxw]['nbA'])) $weekData[$idxm][$idxw]['nbA'] = 0; 
                    if(!isset($monthData[$idxm]['nbA'])) $monthData[$idxm]['nbA'] = 0;
                    
                    foreach($dataSynthese->offsetGet('nbA') as $nba_day) {
                        $orderDate = new \DateTime($nba_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['nbA'] += $nba_day['nbArticle'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['nbA'] += $nba_day['nbArticle'];
                        }
                    }
                    
                    // ECART CAISSE
                    if(!isset($weekData[$idxm][$idxw]['ec'])) $weekData[$idxm][$idxw]['ec'] = 0; 
                    if(!isset($monthData[$idxm]['ec'])) $monthData[$idxm]['ec'] = 0;
                    
                    foreach($dataSynthese->offsetGet('ecDays') as $ec_day) {
                        $orderDate = new \DateTime($ec_day['date']);
                        // Week
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['ec'] += $ec_day['ec'];
                        }
                        // Month
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['ec'] += $ec_day['ec'];
                        }
                    }
                    
                    // ANNULATION CAISSE
                    if(!isset($weekData[$idxm][$idxw]['ac'])) $weekData[$idxm][$idxw]['ac'] = 0; 
                    if(!isset($weekData[$idxm][$idxw]['mac'])) $weekData[$idxm][$idxw]['mac'] = 0;
                    if(!isset($monthData[$idxm]['ac'])) $monthData[$idxm]['ac'] = 0;
                    if(!isset($monthData[$idxm]['mac'])) $monthData[$idxm]['mac'] = 0;
                    
                    foreach($dataSynthese->offsetGet('ac') as $ac_day) {
                        $orderDate = new \DateTime($ac_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['ac'] += $ac_day['ac'];
                            $weekData[$idxm][$idxw]['mac'] += $ac_day['montant_ac'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['ac'] += $ac_day['ac'];
                            $monthData[$idxm]['mac'] += $ac_day['montant_ac'];
                        }
                    }
                    
                    // VENTILATION MODE DE PAIEMENT
                    if(!isset($weekData[$idxm][$idxw]['cb'])) $weekData[$idxm][$idxw]['cb'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['espece'])) $weekData[$idxm][$idxw]['espece'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['cheque'])) $weekData[$idxm][$idxw]['cheque'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['tr'])) $weekData[$idxm][$idxw]['tr'] = 0;
                    
                    if(!isset($monthData[$idxm]['cb'])) $monthData[$idxm]['cb'] = 0;
                    if(!isset($monthData[$idxm]['espece'])) $monthData[$idxm]['espece'] = 0;
                    if(!isset($monthData[$idxm]['cheque'])) $monthData[$idxm]['cheque'] = 0;
                    if(!isset($monthData[$idxm]['tr'])) $monthData[$idxm]['tr'] = 0;
                    foreach($dataSynthese->offsetGet('banque') as $bq_day) {
                        $orderDate = new \DateTime($bq_day['date']);
                        // Week
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $weekData[$idxm][$idxw]['cb'] += $bq_day['cb'];
                            $weekData[$idxm][$idxw]['espece'] += $bq_day['espece'];
                            $weekData[$idxm][$idxw]['cheque'] += $bq_day['cheque'];
                            $weekData[$idxm][$idxw]['tr'] += $bq_day['tr'];
                        }
                        // Month
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['cb'] += $bq_day['cb'];
                            $monthData[$idxm]['espece'] += $bq_day['espece'];
                            $monthData[$idxm]['cheque'] += $bq_day['cheque'];
                            $monthData[$idxm]['tr'] += $bq_day['tr'];
                        }
                    }
                    
                    // REPAS
                    if(!isset($weekData[$idxm][$idxw]['repas'])) $weekData[$idxm][$idxw]['repas'] = 0; 
                    if(!isset($monthData[$idxm]['repas'])) $monthData[$idxm]['repas'] = 0;
                    
                    foreach($dataSynthese->offsetGet('repasDays') as $repas_day) {
                        $orderDate = new \DateTime($repas_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['repas'] += $repas_day['repas'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['repas'] += $repas_day['repas'];
                        }
                    }
                    
                    // Ventilation TVA
                    if(!isset($weekData[$idxm][$idxw]['txreduit1'])) $weekData[$idxm][$idxw]['txreduit1'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['txreduit2'])) $weekData[$idxm][$idxw]['txreduit2'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['txnormal'])) $weekData[$idxm][$idxw]['txnormal'] = 0;
                    
                    if(!isset($monthData[$idxm]['txreduit1'])) $monthData[$idxm]['txreduit1'] = 0;
                    if(!isset($monthData[$idxm]['txreduit2'])) $monthData[$idxm]['txreduit2'] = 0;
                    if(!isset($monthData[$idxm]['txnormal'])) $monthData[$idxm]['txnormal'] = 0;
                    
                    foreach($dataSynthese->offsetGet('vtva') as $vtva_day) {
                        $orderDate = new \DateTime($vtva_day['orderDate']);
                        // Week
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            if($vtva_day['taxeRate']==5.5){
                                $weekData[$idxm][$idxw]['txreduit1'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==7 || $vtva_day['taxeRate']==10){
                                $weekData[$idxm][$idxw]['txreduit2'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==19.6 || $vtva_day['taxeRate']==20){
                                $weekData[$idxm][$idxw]['txnormal'] += $vtva_day['ca_ttc'];
                            }
                        }
                        // Month
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            if($vtva_day['taxeRate']==5.5){
                                $monthData[$idxm]['txreduit1'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==7 || $vtva_day['taxeRate']==10){
                                $monthData[$idxm]['txreduit2'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==19.6 || $vtva_day['taxeRate']==20){
                                $monthData[$idxm]['txnormal'] += $vtva_day['ca_ttc'];
                            }
                        }
                    }
                }
            }
        }
//        var_dump($weekData);
//        var_dump($monthData);
        
        $variables = array(
            'request' => $dataSynthese->request,
            
            'ca' => $dataSynthese->offsetGet('ca'),
            'vtva' => $dataSynthese->offsetGet('vtva'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'echoMonthtoEn' => $this->echoMonthtoEn(),
            'meteoDays' => $dataSynthese->offsetGet('meteoDays'),
            'ecDays' => $dataSynthese->offsetGet('ecDays'), 
            'repasDays' => $dataSynthese->offsetGet('repasDays'),
            'ac' => $dataSynthese->offsetGet('ac'),
            'banque' => $dataSynthese->offsetGet('banque'),
            'rhdata' => $dataSynthese->offsetGet('rhdata'),
            
            'monthData' => $monthData,
            'weekData' => $weekData,
            'budgets' => $dataSynthese->offsetGet('budgets'),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function makegraphiqueAction() {
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'id' => $this->params('id'),
            'ca' => $dataSynthese->offsetGet('ca'),
            'vtva' => $dataSynthese->offsetGet('vtva'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'meteoDays' => $dataSynthese->offsetGet('meteoDays'),
            'ecDays' => $dataSynthese->offsetGet('ecDays'), 
            'repasDays' => $dataSynthese->offsetGet('repasDays'),
            'ac' => $dataSynthese->offsetGet('ac'),
            'banque' => $dataSynthese->offsetGet('banque'),
            'rhdata' => $dataSynthese->offsetGet('rhdata'),
            'budgets' => $dataSynthese->offsetGet('budgets'),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function graphiqueAction() {        
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function detailsAction() {
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca2'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT2'),
            'nbA' => $dataSynthese->offsetGet('nbA2'),
            'period' => $dataSynthese->offsetGet('period'),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
            'echoMonthtoEn' => $this->echoMonthtoEn(),
            'ec' => $dataSynthese->offsetGet('ecDays2'), 
            'repas' => $dataSynthese->offsetGet('repasDays2'),
            'ac' => $dataSynthese->offsetGet('ac2'),
            'banque' => $dataSynthese->offsetGet('banque2'),
            'rhdata' => $dataSynthese->offsetGet('rhdata2'),
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);
        
        return $viewModel;
    }
    
    public function accessrapportAction() {        
        $viewModel = new ViewModel();
        $viewModel->setTerminal(true);
        return $viewModel;
    }
    
    public function rapportproduitAction() {      
        $table = new Table($sm = $this->getServiceLocator());
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'rptProduit' => $dataSynthese->offsetGet('rptProduit'),
            'families' => $table->getSalerowTable()->getFamilies(), //$this->getSalerowTable()->getFamilies(),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
        );        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);        
        return $viewModel;
    }
    
    public function rapportproduit2Action() {      
        $table = new Table($sm = $this->getServiceLocator());
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'rptProduit' => $dataSynthese->offsetGet('rptProduit'),
            'families' => $table->getSalerowTable()->getFamilies(), //$this->getSalerowTable()->getFamilies(),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
        );        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);        
        return $viewModel;
    }
    
    public function rapportproduit3Action() {      
        $table = new Table($sm = $this->getServiceLocator());
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'rptProduit' => $dataSynthese->offsetGet('rptProduit'),
            'families' => $table->getSalerowTable()->getFamilies(), //$this->getSalerowTable()->getFamilies(),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
        );        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);        
        return $viewModel;
    }
	
	public function rapportproduit4Action() {      
        $table = new Table($sm = $this->getServiceLocator());
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'rptProduit' => $dataSynthese->offsetGet('rptProduit'),
            'families' => $table->getSalerowTable()->getFamilies(), //$this->getSalerowTable()->getFamilies(),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
        );        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);        
        return $viewModel;
    }
	
	public function rapportproduit5Action() {      
        $table = new Table($sm = $this->getServiceLocator());
        $dataSynthese = new Container('dataSynthese');
        $variables = array(
            'ca' => $dataSynthese->offsetGet('ca'),
            'tm' => $dataSynthese->offsetGet('tm'),
            'nbT' => $dataSynthese->offsetGet('nbT'),
            'nbA' => $dataSynthese->offsetGet('nbA'),
            'period' => $dataSynthese->offsetGet('period'),
            'rptProduit' => $dataSynthese->offsetGet('rptProduit'),
            'families' => $table->getSalerowTable()->getFamilies(), //$this->getSalerowTable()->getFamilies(),
            'store' => $dataSynthese->offsetGet('store'),
            'monthText' => $this->echoMonthText(),
        );        
        $viewModel = new ViewModel();
        $viewModel->setVariables($variables);
        $viewModel->setTerminal(true);        
        return $viewModel;
    }
    
    public function syntheseexportAction() {
        $table = new Table($sm = $this->getServiceLocator());
        //$operationDate = new OperationDate();
        
        $tableArray = array(
            'marcheStoreTable' => $table->getMarchestoreTable(),
            'marcheTable' => $table->getMarcheTable(),
            'storeTable' => $table->getStoreTable()
        );
        
        $dataSynthese = new Container('dataSynthese');
        $request = $dataSynthese->offsetGet('request');
        $ca = $dataSynthese->offsetGet('ca');
        $vtva = $dataSynthese->offsetGet('vtva');
        $tm = $dataSynthese->offsetGet('tm');
        $nbT = $dataSynthese->offsetGet('nbT');
        $nbA = $dataSynthese->offsetGet('nbA');
        $period = $dataSynthese->offsetGet('period');
        $echoMonthtoEn = $this->echoMonthtoEn();
        $meteoDays = $dataSynthese->offsetGet('meteoDays');
        $ecDays = $dataSynthese->offsetGet('ecDays'); 
        $repasDays = $dataSynthese->offsetGet('repasDays');
        $ac = $dataSynthese->offsetGet('ac');
        $banque = $dataSynthese->offsetGet('banque');
        $rhdata = $dataSynthese->offsetGet('rhdata');
        
        // Get value for month and week
        $monthData = $weekData = array();
        foreach ($dataSynthese->offsetGet('period2') as $idxm => $month) {
            foreach ($month as $idxw => $week) {
                foreach ($week as $idxd => $day) {
                    // CA
                    if(!isset($weekData[$idxm][$idxw]['ca_ttc'])) $weekData[$idxm][$idxw]['ca_ttc'] = 0; 
                    if(!isset($weekData[$idxm][$idxw]['ca_ht'])) $weekData[$idxm][$idxw]['ca_ht'] = 0;
                    if(!isset($monthData[$idxm]['ca_ttc'])) $monthData[$idxm]['ca_ttc'] = 0;
                    if(!isset($monthData[$idxm]['ca_ht'])) $monthData[$idxm]['ca_ht'] = 0;
                        
                    foreach($dataSynthese->offsetGet('ca') as $ca_day) {
                        $orderDate = new \DateTime($ca_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['ca_ttc'] += $ca_day['ca_ttc'];
                            $weekData[$idxm][$idxw]['ca_ht'] += $ca_day['ca_ht'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['ca_ttc'] += $ca_day['ca_ttc'];
                            $monthData[$idxm]['ca_ht'] += $ca_day['ca_ht'];
                        }
                    }
                    
                    // TRANSACTION
                    if(!isset($weekData[$idxm][$idxw]['nbT'])) $weekData[$idxm][$idxw]['nbT'] = 0; 
                    if(!isset($monthData[$idxm]['nbT'])) $monthData[$idxm]['nbT'] = 0;
                    
                    foreach($dataSynthese->offsetGet('nbT') as $nbt_day) {
                        $orderDate = new \DateTime($nbt_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['nbT'] += $nbt_day['nbTransaction'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['nbT'] += $nbt_day['nbTransaction'];
                        }
                    }
                    
                    // ARTICLE
                    if(!isset($weekData[$idxm][$idxw]['nbA'])) $weekData[$idxm][$idxw]['nbA'] = 0; 
                    if(!isset($monthData[$idxm]['nbA'])) $monthData[$idxm]['nbA'] = 0;
                    
                    foreach($dataSynthese->offsetGet('nbA') as $nba_day) {
                        $orderDate = new \DateTime($nba_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['nbA'] += $nba_day['nbArticle'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['nbA'] += $nba_day['nbArticle'];
                        }
                    }
                    
                    // ECART CAISSE
                    if(!isset($weekData[$idxm][$idxw]['ec'])) $weekData[$idxm][$idxw]['ec'] = 0; 
                    if(!isset($monthData[$idxm]['ec'])) $monthData[$idxm]['ec'] = 0;
                    
                    foreach($dataSynthese->offsetGet('ecDays') as $ec_day) {
                        $orderDate = new \DateTime($ec_day['date']);
                        // Week
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['ec'] += $ec_day['ec'];
                        }
                        // Month
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['ec'] += $ec_day['ec'];
                        }
                    }
                    
                    // ANNULATION CAISSE
                    if(!isset($weekData[$idxm][$idxw]['ac'])) $weekData[$idxm][$idxw]['ac'] = 0; 
                    if(!isset($weekData[$idxm][$idxw]['mac'])) $weekData[$idxm][$idxw]['mac'] = 0;
                    if(!isset($monthData[$idxm]['ac'])) $monthData[$idxm]['ac'] = 0;
                    if(!isset($monthData[$idxm]['mac'])) $monthData[$idxm]['mac'] = 0;
                    
                    foreach($dataSynthese->offsetGet('ac') as $ac_day) {
                        $orderDate = new \DateTime($ac_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['ac'] += $ac_day['ac'];
                            $weekData[$idxm][$idxw]['mac'] += $ac_day['montant_ac'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['ac'] += $ac_day['ac'];
                            $monthData[$idxm]['mac'] += $ac_day['montant_ac'];
                        }
                    }
                    
                    // VENTILATION MODE DE PAIEMENT
                    if(!isset($weekData[$idxm][$idxw]['cb'])) $weekData[$idxm][$idxw]['cb'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['espece'])) $weekData[$idxm][$idxw]['espece'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['cheque'])) $weekData[$idxm][$idxw]['cheque'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['tr'])) $weekData[$idxm][$idxw]['tr'] = 0;
                    
                    if(!isset($monthData[$idxm]['cb'])) $monthData[$idxm]['cb'] = 0;
                    if(!isset($monthData[$idxm]['espece'])) $monthData[$idxm]['espece'] = 0;
                    if(!isset($monthData[$idxm]['cheque'])) $monthData[$idxm]['cheque'] = 0;
                    if(!isset($monthData[$idxm]['tr'])) $monthData[$idxm]['tr'] = 0;
                    foreach($dataSynthese->offsetGet('banque') as $bq_day) {
                        $orderDate = new \DateTime($bq_day['date']);
                        // Week
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $weekData[$idxm][$idxw]['cb'] += $bq_day['cb'];
                            $weekData[$idxm][$idxw]['espece'] += $bq_day['espece'];
                            $weekData[$idxm][$idxw]['cheque'] += $bq_day['cheque'];
                            $weekData[$idxm][$idxw]['tr'] += $bq_day['tr'];
                        }
                        // Month
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['cb'] += $bq_day['cb'];
                            $monthData[$idxm]['espece'] += $bq_day['espece'];
                            $monthData[$idxm]['cheque'] += $bq_day['cheque'];
                            $monthData[$idxm]['tr'] += $bq_day['tr'];
                        }
                    }
                    
                    // REPAS
                    if(!isset($weekData[$idxm][$idxw]['repas'])) $weekData[$idxm][$idxw]['repas'] = 0; 
                    if(!isset($monthData[$idxm]['repas'])) $monthData[$idxm]['repas'] = 0;
                    
                    foreach($dataSynthese->offsetGet('repasDays') as $repas_day) {
                        $orderDate = new \DateTime($repas_day['orderDate']);
                        // Week                        
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')){
                            $weekData[$idxm][$idxw]['repas'] += $repas_day['repas'];
                        }
                        // Month                        
                        if($orderDate->format(('Y-m'))==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            $monthData[$idxm]['repas'] += $repas_day['repas'];
                        }
                    }
                    
                    // Ventilation TVA
                    if(!isset($weekData[$idxm][$idxw]['txreduit1'])) $weekData[$idxm][$idxw]['txreduit1'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['txreduit2'])) $weekData[$idxm][$idxw]['txreduit2'] = 0;
                    if(!isset($weekData[$idxm][$idxw]['txnormal'])) $weekData[$idxm][$idxw]['txnormal'] = 0;
                    
                    if(!isset($monthData[$idxm]['txreduit1'])) $monthData[$idxm]['txreduit1'] = 0;
                    if(!isset($monthData[$idxm]['txreduit2'])) $monthData[$idxm]['txreduit2'] = 0;
                    if(!isset($monthData[$idxm]['txnormal'])) $monthData[$idxm]['txnormal'] = 0;
                    
                    foreach($dataSynthese->offsetGet('vtva') as $vtva_day) {
                        $orderDate = new \DateTime($vtva_day['orderDate']);
                        // Week
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('W')==$idxw && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            if($vtva_day['taxeRate']==5.5){
                                $weekData[$idxm][$idxw]['txreduit1'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==7 || $vtva_day['taxeRate']==10){
                                $weekData[$idxm][$idxw]['txreduit2'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==19.6 || $vtva_day['taxeRate']==20){
                                $weekData[$idxm][$idxw]['txnormal'] += $vtva_day['ca_ttc'];
                            }
                        }
                        // Month
                        if($orderDate->format('Y-m')==$idxm && $orderDate->format('Y-m-d')==$day->format('Y-m-d')) {
                            if($vtva_day['taxeRate']==5.5){
                                $monthData[$idxm]['txreduit1'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==7 || $vtva_day['taxeRate']==10){
                                $monthData[$idxm]['txreduit2'] += $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==19.6 || $vtva_day['taxeRate']==20){
                                $monthData[$idxm]['txnormal'] += $vtva_day['ca_ttc'];
                            }
                        }
                    }
                }
            }
        }
        
        $filename = 'Synthese-'.time();
        $excelManager = new ExcelManager($tableArray);
        $excelManager->load();
        
        // Insertion des données dans le fichier excel
        $ligne = 2;
        foreach ($period as $idxm => $month) {            
            $data = array();
            $monthExpload = explode(" ", $idxm);
            $monthName = $monthExpload[0];
            switch ($monthName) {
                case 'Janvier':
                    $monthIdx = $monthExpload[1].'-01';
                    break;
                case 'Février':
                    $monthIdx = $monthExpload[1].'-02';
                    break;
                case 'Mars':
                    $monthIdx = $monthExpload[1].'-03';
                    break;
                case 'Avril':
                    $monthIdx = $monthExpload[1].'-04';
                    break;
                case 'Mai':
                    $monthIdx = $monthExpload[1].'-05';
                    break;
                case 'Juin':
                    $monthIdx = $monthExpload[1].'-06';
                    break;
                case 'Juillet':
                    $monthIdx = $monthExpload[1].'-07';
                    break;
                case 'Août':
                    $monthIdx = $monthExpload[1].'-08';
                    break;
                case 'Septembre':
                    $monthIdx = $monthExpload[1].'-09';
                    break;
                case 'Octobre':
                    $monthIdx = $monthExpload[1].'-10';
                    break;
                case 'Novembre':
                    $monthIdx = $monthExpload[1].'-11';
                    break;
                case 'Décembre':
                    $monthIdx = $monthExpload[1].'-12';
                    break;

                default:
                    break;
            }
            $monthTxPrise = 0;
            if($monthData[$monthIdx]['nbT'] > 0)
                $monthTxPrise = $monthData[$monthIdx]['nbA']/$monthData[$monthIdx]['nbT'];

            $monthTm = 0;
            if($monthData[$monthIdx]['nbT'] > 0)
                $monthTm = $monthData[$monthIdx]['ca_ttc']/$monthData[$monthIdx]['nbT'];
            
            /////////////////////////////////////////////////////////////////////////////////
            
            $data[] = $monthName;
            
            $data[] = number_format($monthData[$monthIdx]['ca_ttc'],2,',', ' ');
            $data[] = number_format($monthData[$monthIdx]['ca_ht'],2,',', ' ');
            
            $data[] = number_format($monthData[$monthIdx]['txreduit1'],2,',', ' ');            
            $data[] = number_format($monthData[$monthIdx]['txreduit2'],2,',', ' ');            
            $data[] = number_format($monthData[$monthIdx]['txnormal'],2,',', ' ');  
            
            $data[] = number_format($monthData[$monthIdx]['cb'],2,',', ' ');            
            $data[] = number_format($monthData[$monthIdx]['espece'],2,',', ' ');            
            $data[] = '';            
            $data[] = '';            
            $data[] = '';      
            $data[] = number_format($monthData[$monthIdx]['cheque'],2,',', ' '); 
            $data[] = '';
            $data[] = number_format($monthData[$monthIdx]['tr'],2,',', ' '); 
            $data[] = '';
            
            $data[] = '';
            
            $data[] = '';
            $data[] = '';
            
            $data[] = number_format($monthData[$monthIdx]['ac'],0,',', ' ');
            $data[] = number_format($monthData[$monthIdx]['mac'],2,',', ' ');
            $data[] = '';
            $data[] = '';
            $data[] = number_format($monthData[$monthIdx]['ec'],2,',', ' ');
            $data[] = '';
            $data[] = '';
            $data[] = '';
            
            $data[] = '';
            $data[] = '';
            
            $data[] = number_format($monthData[$monthIdx]['nbT'],0,',', ' ');
            $data[] = number_format($monthTm,2,',', ' ');
            $data[] = number_format($monthData[$monthIdx]['nbA'],0,',', ' ');
            $data[] = number_format($monthTxPrise,2,',', ' ');
            
            $data[] = '';
            $data[] = '';
            $data[] = '';
            $data[] = '';
            $data[] = '';
            $data[] = '';            
            
            $data[] = '';
            $data[] = '';
            $data[] = '';
            $data[] = '';
            $data[] = '';
            $data[] = '';
            
            $data[] = '';
            $data[] = '';
            $data[] = number_format($monthData[$monthIdx]['repas'],0,',', ' ');
            $data[] = '';
            $data[] = '';
            $data[] = '';
            
            $data[] = '';
            $data[] = '';
            
            $excelManager->fillExcel($data, $ligne, 'month');
            $ligne++;
            
            // Boucle sur les semaines
            $y = 1; 
            foreach ($month as $idxw => $week) {
                $data = array();
                $weekTxPrise = 0;
                if($weekData[$monthIdx][$idxw]['nbT'] > 0)
                    $weekTxPrise = $weekData[$monthIdx][$idxw]['nbA']/$weekData[$monthIdx][$idxw]['nbT'];

                $weekTm = 0;
                if($weekData[$monthIdx][$idxw]['nbT'] > 0)
                    $weekTm = $weekData[$monthIdx][$idxw]['ca_ttc']/$weekData[$monthIdx][$idxw]['nbT'];
                
                /////////////////////////////////////////////////////////////////////////////////
            
                $data[] = 'Semaine '.$y;

                $data[] = number_format($weekData[$monthIdx][$idxw]['ca_ttc'],2,',', ' ');
                $data[] = number_format($weekData[$monthIdx][$idxw]['ca_ht'],2,',', ' ');

                $data[] = number_format($weekData[$monthIdx][$idxw]['txreduit1'],2,',', ' ');            
                $data[] = number_format($weekData[$monthIdx][$idxw]['txreduit2'],2,',', ' ');            
                $data[] = number_format($weekData[$monthIdx][$idxw]['txnormal'],2,',', ' ');  

                $data[] = number_format($weekData[$monthIdx][$idxw]['cb'],2,',', ' ');            
                $data[] = number_format($weekData[$monthIdx][$idxw]['espece'],2,',', ' ');            
                $data[] = '';            
                $data[] = '';            
                $data[] = '';      
                $data[] = number_format($weekData[$monthIdx][$idxw]['cheque'],2,',', ' '); 
                $data[] = '';
                $data[] = number_format($weekData[$monthIdx][$idxw]['tr'],2,',', ' '); 
                $data[] = '';

                $data[] = '';

                $data[] = '';
                $data[] = '';

                $data[] = number_format($weekData[$monthIdx][$idxw]['ac'],0,',', ' ');
                $data[] = number_format($weekData[$monthIdx][$idxw]['mac'],2,',', ' ');
                $data[] = '';
                $data[] = '';
                $data[] = number_format($weekData[$monthIdx][$idxw]['ec'],2,',', ' ');
                $data[] = '';
                $data[] = '';
                $data[] = '';

                $data[] = '';
                $data[] = '';

                $data[] = number_format($weekData[$monthIdx][$idxw]['nbT'],0,',', ' ');
                $data[] = number_format($weekTm,2,',', ' ');
                $data[] = number_format($weekData[$monthIdx][$idxw]['nbA'],0,',', ' ');
                $data[] = number_format($weekTxPrise,2,',', ' ');

                $data[] = '';
                $data[] = '';
                $data[] = '';
                $data[] = '';
                $data[] = '';
                $data[] = '';            

                $data[] = '';
                $data[] = '';
                $data[] = '';
                $data[] = '';
                $data[] = '';
                $data[] = '';

                $data[] = '';
                $data[] = '';
                $data[] = number_format($weekData[$monthIdx][$idxw]['repas'],0,',', ' ');
                $data[] = '';
                $data[] = '';
                $data[] = '';

                $data[] = '';
                $data[] = '';

                $excelManager->fillExcel($data, $ligne, 'week');
                $ligne++;
                
                // Boucle sur les jours
                foreach ($week as $idxd => $day) {
                    $data = array();
                    
                    // Get CA for each day
                    $ca_ttc = $ca_ht =0;
                    foreach($ca as $ca_day):
                        if($day->format('Y-m-d')==$ca_day['orderDate']){
                            $ca_ttc = $ca_day['ca_ttc'];
                            $ca_ht = $ca_day['ca_ht'];
                        }
                    endforeach;

                    // Get TM for each day
                    $nb = $val = $nbC = $valC = $nbE = $valE = 0;
                    foreach($tm as $tm_day):
                        if($day->format('Y-m-d')==$tm_day['orderDate']){                        
                            $nb += @$tm_day['nombre'];
                            $val += @$tm_day['valeur'];
//                            if($tm_day['type'] == 'C'){                        
//                                $nbC += @$tm_day['nombre'];
//                                $valC += @$tm_day['valeur'];
//                            }
//                            if($tm_day['type'] == 'E'){                        
//                                $nbE += @$tm_day['nombre'];
//                                $valE += @$tm_day['valeur'];
//                            }
                        }
                    endforeach;
                    $tm_ttc = ($nb!=0)?($val/$nb):0;
//                    $c_tm_ttc = ($nbC!=0)?($valC/$nbC):0;
//                    $e_tm_ttc = ($nbE!=0)?($valE/$nbE):0;

                    // Get nbT for each day
                     $nbTra = 0;
                    foreach($nbT as $nbT_day):
                        if($day->format('Y-m-d')==$nbT_day['orderDate']){
                            $nbTra = ($nbT_day['nbTransaction']!=0)?$nbT_day['nbTransaction']:0;
                        }
                    endforeach;

                    // Get nbA for each day
                    $nbArt = 0; 
                    foreach($nbA as $nbA_day):
                        if($day->format('Y-m-d')==$nbA_day['orderDate']){
                            $nbArt = ($nbA_day['nbArticle']!=0)?$nbA_day['nbArticle']:0;
                        }
                    endforeach;

                    // Get Taux de prise
                    $tx = 0;
                    if($nbTra > 0)
                        $tx = $nbArt/$nbTra;

                    // Get AnnulationCaisse for each day
                    $nbAc = $mtAc= 0;
                    foreach($ac as $ac_day):
                        if($day->format('Y-m-d')==$ac_day['orderDate']){
                            $nbAc = ($ac_day['ac']!=0)?$ac_day['ac']:0;
                            $mtAc = ($ac_day['montant_ac']!=0)?$ac_day['montant_ac']:0;
                        }
                    endforeach;

                    // Get meteo for each day
                    $meteo = "&nbsp;";
                    $temperature = "&nbsp;";
                    foreach($meteoDays as $meteoDay){                                                                        
                        if($day->format('Y-m-d')==$meteoDay['meteoDate']){                                        
                            $meteo = $meteoDay['type'];
                            $temperature = number_format($meteoDay['temperature'],1,',', ' ');
                        }
                    }

                    // Get ecarts caisse for each day
                    $ec = 0; 
                    foreach($ecDays as $ec_day):
                        if($day->format('Y-m-d')==$ec_day['date']){
                            $ec = ($ec_day['ec']!=0)?$ec_day['ec']:0;
                        }
                    endforeach;

                    // Get MO for each day
                    $mo = 0; 
                    foreach($rhdata as $data):
                        if($day->format('Y-m-d')==$data['date']){
                            $mo = ($data['mo']!=0)?$data['mo']:0;
                        }
                    endforeach;

                    // Get Hours for each day
                    $nbh = 0; 
                    foreach($rhdata as $data):
                        if($day->format('Y-m-d')==$data['date']){
                            $nbh = ($data['nbh']!=0)?$data['nbh']:0;
                        }
                    endforeach;

                    // Get Productivity for each day
                    $prod = 0; 
                    foreach($rhdata as $data):
                        if($day->format('Y-m-d')==$data['date']){
                            $prod = ($data['prod']!=0)?$data['prod']:0;
                        }
                    endforeach;

                    // Get Transfert input for each day
                    $te = 0; 
                    foreach($rhdata as $data):
                        if($day->format('Y-m-d')==$data['date']){
                            $te = ($data['te']!=0)?$data['te']:0;
                        }
                    endforeach;

                    // Get Transfert output for each day
                    $ts = 0; 
                    foreach($rhdata as $data):
                        if($day->format('Y-m-d')==$data['date']){
                            $ts = ($data['ts']!=0)?$data['ts']:0;
                        }
                    endforeach;

                    // Get meal count for each day
                    $repas = 0;
                    foreach($repasDays as $repas_day):
                        if($day->format('Y-m-d')==$repas_day['orderDate']){
                            $repas = ($repas_day['repas']!=0)?$repas_day['repas']:0;
                        }
                    endforeach;

                    // Get payment ventilation for each day
                    $cb = $espece = $cheque = $tr = 0;
                    foreach($banque as $banque_day):
                        if($day->format('Y-m-d')==$banque_day['date']){
                            $cb = ($banque_day['cb']!=0)?$banque_day['cb']:0;
                            $cheque = ($banque_day['cheque']!=0)?$banque_day['cheque']:0;
                            $espece = ($banque_day['espece']!=0)?$banque_day['espece']:0;
                            $tr = ($banque_day['tr']!=0)?$banque_day['tr']:0;
                        }
                    endforeach;

                    // Get tva ventilation for each day
                    $txreduit1 = $txreduit2 = $txnormal = 0;
                    foreach($vtva as $vtva_day):
                        if($day->format('Y-m-d')==$vtva_day['orderDate']){
                            if($vtva_day['taxeRate']==5.5){
                                $txreduit1 = $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==7 || $vtva_day['taxeRate']==10){
                                $txreduit2 = $vtva_day['ca_ttc'];
                            }
                            if($vtva_day['taxeRate']==19.6 || $vtva_day['taxeRate']==20){
                                $txnormal = $vtva_day['ca_ttc'];
                            }
                        }
                    endforeach;
                    
                    /////////////////////////////////////////////////////////////////////////////////
                    $daynum = ($day->format('N')==7)?0:$day->format('N');
                    if(in_array($daynum, $request->semaine)):
                        $data[] = $idxd.' '.$day->format('d-M-y');

                        $data[] = number_format($ca_ttc,2,',', ' ');
                        $data[] = number_format($ca_ht,2,',', ' ');

                        $data[] = number_format($txreduit1,2,',', ' ');            
                        $data[] = number_format($txreduit2,2,',', ' ');            
                        $data[] = number_format($txnormal,2,',', ' ');  

                        $data[] = number_format($cb,2,',', ' ');            
                        $data[] = number_format($espece,2,',', ' ');            
                        $data[] = '';            
                        $data[] = '';            
                        $data[] = '';      
                        $data[] = number_format($cheque,2,',', ' '); 
                        $data[] = '';
                        $data[] = number_format($tr,2,',', ' '); 
                        $data[] = '';

                        $data[] = '';

                        $data[] = '';
                        $data[] = '';

                        $data[] = number_format($nbAc,0,',', ' ');
                        $data[] = number_format($mtAc,2,',', ' ');
                        $data[] = '';
                        $data[] = '';
                        $data[] = number_format($ec,2,',', ' ');
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';

                        $data[] = '';
                        $data[] = '';

                        $data[] = number_format($nbTra,0,',', ' ');
                        $data[] = number_format($tm_ttc,2,',', ' ');
                        $data[] = number_format($nbArt,0,',', ' ');
                        $data[] = number_format($tx,2,',', ' ');

                        $data[] = '';
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';            

                        $data[] = number_format($mo,2,',', ' ');
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';

                        $data[] = '';
                        $data[] = '';
                        $data[] = number_format($repas,0,',', ' ');
                        $data[] = '';
                        $data[] = '';
                        $data[] = '';

                        $data[] = $meteo;
                        $data[] = $temperature;

                        $excelManager->fillExcel($data, $ligne, 'day');
                        $ligne++;
                    endif;
                }
                $y++;
            }
        }
        
        $excelManager->save($filename.'.xls');
        
        return new \Zend\View\Model\JsonModel(array(
            'file' => $filename.'.xls'
        ));
    }
    
    public function echoShortDayText()
    {
        $dayText[0] = 'dim';
        $dayText[1] = 'lun';
        $dayText[2] = 'mar';
        $dayText[3] = 'mer';
        $dayText[4] = 'jeu';
        $dayText[5] = 'ven';
        $dayText[6] = 'sam';        
        
        return $dayText;
    }
    
    public function echoLongDayText()
    {
        $dayText[0] = 'dimanche';
        $dayText[1] = 'lundi';
        $dayText[2] = 'mardi';
        $dayText[3] = 'mercredi';
        $dayText[4] = 'jeudi';
        $dayText[5] = 'vendredi';
        $dayText[6] = 'samedi';        
        
        return $dayText;
    }
    
    public function echoMonthText()
    {
        $monthText[1] = 'Janvier';
        $monthText[2] = 'Février';
        $monthText[3] = 'Mars';
        $monthText[4] = 'Avril';
        $monthText[5] = 'Mai';
        $monthText[6] = 'Juin';
        $monthText[7] = 'Juillet';
        $monthText[8] = 'Août';
        $monthText[9] = 'Septembre';
        $monthText[10] = 'Octobre';
        $monthText[11] = 'Novembre';
        $monthText[12] = 'Décembre';
        
        return $monthText;
    }
    
    public function echoMonthtoEn()
    {
        $monthText['Janvier'] = 'January';
        $monthText['Février'] = 'February';
        $monthText['Mars'] = 'March';
        $monthText['Avril'] = 'April';
        $monthText['Mai'] = 'May';
        $monthText['Juin'] = 'June';
        $monthText['Juillet'] = 'July';
        $monthText['Août'] = 'August';
        $monthText['Septembre'] = 'September';
        $monthText['Octobre'] = 'October';
        $monthText['Novembre'] = 'November';
        $monthText['Décembre'] = 'December';
        
        return $monthText;
    }
    
    
}
?>
